package ex;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import connDB.ConnDB;

public class EX04_BooksDAO implements EX04_IBooksDAO{

	
	private ConnDB cDB = new ConnDB();
	//private String DRV = "oracle.jdbc.driver.OracleDriver";
	//private String URL = "jdbc:oracle:thin:gdori/gdori@112.216.253.125:1251:xe";
	private String insertSQL = "";
	private String selectSQL = "";
	private String updateSQL = "";
	private String deleteSQL = "";
	
	
	@Override
	public boolean insertBooks(EX04_BooksVO bvo) {
		// TODO Auto-generated method stub
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		
		try {
			
			conn = cDB.openDataBase();
			pstmt = conn.prepareStatement(insertSQL);
			pstmt.setInt(1,bvo.getBooksNo());
			pstmt.setString(2, bvo.getBooksCode());
			pstmt.setString(3,bvo.getBooksName());
			pstmt.setString(4,bvo.getBooksWriter());
			
			pstmt.executeUpdate();
			result = true;
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			cDB.closeDataBase(conn,pstmt,null);
		}
		return result;
	}
	
	@Override
	public List<EX04_BooksVO> selectBooks() {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<EX04_BooksVO> result = new ArrayList<>();
		
		try {
			conn = cDB.openDataBase();
			pstmt = conn.prepareStatement(selectSQL);
			pstmt.executeQuery();
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				EX04_BooksVO bvo = new EX04_BooksVO();
				bvo.setBooksNo(rs.getInt("books_no"));
				bvo.setBooksCode(rs.getString("books_code"));
				bvo.setBooksName(rs.getString("books_name"));
				bvo.setBooksWriter(rs.getString("books_writer"));
				
				result.add(bvo);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			cDB.closeDataBase(conn, pstmt, rs);
		}
		
		return result;
	}
	
	@Override
	public boolean updateBooks(EX04_BooksVO bvo) {
		// TODO Auto-generated method stub
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		
		try {
			conn = cDB.openDataBase();
			pstmt = conn.prepareStatement(updateSQL);
			
			pstmt.setInt(1, bvo.getBooksNo());
			pstmt.setString(2,bvo.getBooksCode());
			pstmt.setString(3,bvo.getBooksName());
			pstmt.setString(4,bvo.getBooksWriter());
			
			int cnt = pstmt.executeUpdate();;
			if(cnt == 0){
				System.out.println("실행됨 수정은 안됨");
			}else{
				result = true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			cDB.closeDataBase(conn, pstmt, null);
		}
		
		return result;
	}
	
	@Override
	public boolean deleteBooks(EX04_BooksVO bvo) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		
		try {
			conn = cDB.openDataBase();
			pstmt = conn.prepareStatement(deleteSQL);
			pstmt.setString(1, bvo.getBooksCode());;
			
			int cnt = pstmt.executeUpdate();
			if(cnt == 0){
				System.out.println("실행됨 삭제안됨");
			}else{
				result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			cDB.closeDataBase(conn, pstmt, null);
		}
		
		return result;
	}
	/*
	private Connection openDataBase(){
		
		try {
			Class.forName(DRV);
			return DriverManager.getConnection(URL);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	private void closeDataBase(Connection conn, PreparedStatement pstmt ,ResultSet rs){
		if(rs != null){
			try {
				rs.close();
				rs = null;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		
		if(pstmt != null){
			try {
				pstmt.close();
				pstmt = null;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		
		if(conn != null){
			try {
				conn.close();
				conn = null;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
	*/
}
